package com.aier.school.action;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.common.dao.ICommonDAO;
import com.aier.school.common.SchoolFinalPageSize;
import com.aier.school.common.TeacherLeaveType;
import com.aier.school.entity.SchoolGrade;
import com.aier.school.entity.SchoolTeacher;
import com.aier.school.entity.TeacherLeave;
import com.aier.school.service.ISchoolLeaveService;
import com.aier.school.service.ISchoolService;
import com.aier.school.service.ISchoolTeacherService;

@Scope("prototype")
@Component("schoolLeaveManageAction")
public class SchoolLeaveManageAction extends SchoolBaseAction {

    /**
     * 学校管理-管理员请假管理  与  教师请假管理
     */
    private static final long serialVersionUID = 1L;
    @Resource(name = "schoolLeaveService")
    private ISchoolLeaveService schoolLeaveService;
    @Resource(name = "schoolService")
    private ISchoolService schoolService;    
    @Resource(name = "schoolTeacherService")
    private ISchoolTeacherService schoolTeacherService;        
    
    private int s;                //成功:1 ,失败:0
    private String error_info;    //错误提示信息

    private List<String> tnameAccountList;        //教师名字账号列表
    private List<SchoolGrade> schoolGradeList;    //职务列表    
    private List<SchoolTeacher> schoolTeacherList;        //学校教师列表    
    private List<TeacherLeave> terLeaves;        //教师请假列表
    
    private Integer sid;        //学校id
    private Long tid;            //教师id
    private Long gid;            //职务id
    private String account;        //教师账号

    private String txt_sdate;    //开始时间
    private String txt_edate;    //结束时间
    private short state_leave;    //请假状态
    private int type_handle;    //请假类型
    private Long lid;            //请假id
    private String txt_desinfo;    //请假事由
    private Integer type_resone;//请假原因类型
    
    private int page;            //页数
    private int page_size;        //每页的记录数
    private int page_count;        //总页数
    private int count;            //总数
    
    private Map<Integer,String> leaveTypeResoneMap;
    private Map<Integer,String> leaveExceptionResoneMap;
    private Map<Integer,String> leaveTypeHandleMap;

    @Override
    public String execute(){
        tnameAccountList = schoolTeacherService.searchTNameAndAccountBySid(sid);
        tnameAccountList.add(0, "请选择");
        schoolGradeList = schoolService.searchSchoolGradeList(sid);
        leaveTypeResoneMap = TeacherLeaveType.leaveTypeResoneMap;
        leaveExceptionResoneMap = TeacherLeaveType.leaveExceptionResoneMap;
        leaveTypeHandleMap = TeacherLeaveType.leaveTypeHandleMap;
        
        return SUCCESS;
    }
    
    //查询请假信息  学校管理员
    @SuppressWarnings("unchecked")
    public String searchLeaList_admin(){
        s = 0;
        if(page<=0){
            page=1;
        }
        List<Long> tids = new ArrayList<Long>();    
        List<SchoolTeacher> teachers = schoolTeacherService.selectTeacher(sid,account,gid);
        if(teachers!=null&&teachers.size()!=0){
            for(SchoolTeacher teacher:teachers){
                tids.add(teacher.getTid());
            }
            Map<String,Object> map = schoolLeaveService.searchTerLeavesByCondition(page,SchoolFinalPageSize.TEACHER_LEAVE_LIST_PAGESIZE,
                   sid,tids,type_handle,txt_sdate,txt_edate);
            if(null!=map){
                terLeaves = (List<TeacherLeave>) map.get(ICommonDAO.OBJ_LIST);
                if (null != terLeaves) {
                    s = 1;
                }
                page = (Integer) map.get(ICommonDAO.PAGE);
                page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
                page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
                count = (Integer) map.get(ICommonDAO.COUNT);
            }
        }
        return SUCCESS;
    }

    //查询请假信息  学校普通教师
    @SuppressWarnings("unchecked")
    public String searchLeaList_normal(){
        s = 0;
        if(page<=0){
            page=1;
        }
        List<Long> tids = new ArrayList<Long>();    
        tids.add(tid);
        Map<String,Object> map = schoolLeaveService.searchTerLeavesByCondition(page,SchoolFinalPageSize.TEACHER_LEAVE_LIST_PAGESIZE,
               sid,tids,type_handle,txt_sdate,txt_edate);
        if(null!=map){
            terLeaves = (List<TeacherLeave>) map.get(ICommonDAO.OBJ_LIST);
            if (null != terLeaves) {
                s = 1;
            }
            page = (Integer) map.get(ICommonDAO.PAGE);
            page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
        }
        return SUCCESS;
    }
    
    // 添加请假信息 管理员
    public String add_admin() {
        try {
            Timestamp sdate = Timestamp.valueOf(txt_sdate);
            Timestamp edate = Timestamp.valueOf(txt_edate);
            txt_desinfo = TeacherLeaveType.leaveTypeResoneMap.get(type_resone)+":"+txt_desinfo;
            s = schoolLeaveService.insertTeacherLeave(schoolTeacherService.searchSchoolTeacherByAccount(account,sid).getTid(), 
                    sdate, edate, sid,txt_desinfo,TeacherLeaveType.STATE_SH,TeacherLeaveType.TYPE_HABDLE_NJ);    
        } catch (Exception e) {
            e.printStackTrace();
            s = 0;
        }
        return SUCCESS;
    }

    // 添加请假信息 普通教师
    public String add_normal() {
        try {
            Timestamp sdate = Timestamp.valueOf(txt_sdate);
            Timestamp edate = Timestamp.valueOf(txt_edate);
            txt_desinfo = TeacherLeaveType.leaveTypeResoneMap.get(type_resone)+":"+txt_desinfo;
            s = schoolLeaveService.insertTeacherLeave(tid, 
                    sdate, edate, sid,txt_desinfo,TeacherLeaveType.STATE_SH,TeacherLeaveType.TYPE_HABDLE_NJ);    
        } catch (Exception e) {
            e.printStackTrace();
            s = 0;
        }
        return SUCCESS;
    }
    
    
    //  修改请假状态
    public String update() {
        s = schoolLeaveService.updateTeacherLeave(lid,state_leave,tid,null);
        return SUCCESS;
    }
    
    // 删除请假信息
    public String delete() {
        s = schoolLeaveService.deleteTeacherLeave(lid);
        return SUCCESS;
    }
    
    // 补假
    public String additional() {
        try {
            Timestamp sdate = Timestamp.valueOf(txt_sdate);
            Timestamp edate = Timestamp.valueOf(txt_edate);
            Long tid_add = schoolTeacherService.searchSchoolTeacherByAccount(account,sid).getTid();
            txt_desinfo = TeacherLeaveType.leaveTypeResoneMap.get(type_resone)+":"+txt_desinfo;
            schoolLeaveService.insertTeacherLeave( tid_add,sdate, edate, sid,txt_desinfo,TeacherLeaveType.STATE_CL,
                    TeacherLeaveType.TYPE_HABDLE_SJ,tid);    
            schoolLeaveService.updateTeacherAttendanceMonth(tid_add, sdate, edate);        
            s = 1;
        } catch (Exception e) {
            e.printStackTrace();
            s = 0;
        }
        return SUCCESS;
    }
    
    //选择职务
    public String changetype(){
        if(gid==-1){
            tnameAccountList = schoolTeacherService.searchTNameAndAccountBySid(sid);
        }else{
            tnameAccountList = schoolTeacherService.searchTNameAndAccountBySidGid(gid,sid);          
        }

        return SUCCESS;
    }  
    
    //添加异常请假信息
    public String addException() {
        s = 1;
        try {
            Timestamp sdate = Timestamp.valueOf(txt_sdate);
            Timestamp edate = Timestamp.valueOf(txt_edate);
            List<Long> tids = new ArrayList<Long>();
            List<SchoolTeacher> teachers = schoolTeacherService.selectTeacher(sid,account,gid);
            if(teachers!=null&&teachers.size()!=0){
                for(SchoolTeacher teacher:teachers){
                    tids.add(teacher.getTid());
                }
                s = schoolLeaveService.insertTeacherLeaves( tids,sdate, edate, sid,txt_desinfo,TeacherLeaveType.STATE_CL,
                    TeacherLeaveType.TYPE_HABDLE_BJ,tid);                 
            }
        } catch (Exception e) {
            e.printStackTrace();
            s = 0;
        }
        return SUCCESS;
    }

    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public Long getTid() {
        return tid;
    }

    public void setTid(Long tid) {
        this.tid = tid;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPage_size() {
        return page_size;
    }

    public void setPage_size(int page_size) {
        this.page_size = page_size;
    }

    public int getPage_count() {
        return page_count;
    }

    public void setPage_count(int page_count) {
        this.page_count = page_count;
    }

    public int getS() {
        return s;
    }

    public String getError_info() {
        return error_info;
    }

    public List<String> getTnameAccountList() {
        return tnameAccountList;
    }

    public List<SchoolGrade> getSchoolGradeList() {
        return schoolGradeList;
    }

    public List<SchoolTeacher> getSchoolTeacherList() {
        return schoolTeacherList;
    }

    public List<TeacherLeave> getTerLeaves() {
        return terLeaves;
    }

    public int getCount() {
        return count;
    }

    public Map<Integer, String> getLeaveTypeResoneMap() {
        return leaveTypeResoneMap;
    }

    public Map<Integer, String> getLeaveExceptionResoneMap() {
        return leaveExceptionResoneMap;
    }

    public Map<Integer, String> getLeaveTypeHandleMap() {
        return leaveTypeHandleMap;
    }

    public void setSchoolLeaveService(ISchoolLeaveService schoolLeaveService) {
        this.schoolLeaveService = schoolLeaveService;
    }

    public void setSchoolService(ISchoolService schoolService) {
        this.schoolService = schoolService;
    }

    public void setSchoolTeacherService(ISchoolTeacherService schoolTeacherService) {
        this.schoolTeacherService = schoolTeacherService;
    }

    public void setGid(Long gid) {
        this.gid = gid;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public void setTxt_sdate(String txt_sdate) {
        this.txt_sdate = txt_sdate;
    }

    public void setTxt_edate(String txt_edate) {
        this.txt_edate = txt_edate;
    }

    public void setState_leave(short state_leave) {
        this.state_leave = state_leave;
    }

    public void setType_handle(int type_handle) {
        this.type_handle = type_handle;
    }

    public void setLid(Long lid) {
        this.lid = lid;
    }

    public void setTxt_desinfo(String txt_desinfo) {
        this.txt_desinfo = txt_desinfo;
    }

    public void setType_resone(Integer type_resone) {
        this.type_resone = type_resone;
    }

    
    

}
